# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     DriverObj.py
   Description :
   Author :        ZhangYiLin
   date：          2020/12/09 09:55
-------------------------------------------------
   Change Activity:
                   2020/12/09 09:55
-------------------------------------------------
"""
import re


def custom_manage(custom_fail, relevance):
    """
    自定义关联配置
    :param custom_fail:  自定义错误说明
    :param relevance:  关联对象
    :return:
    """
    try:
        relevance_list = re.findall(
            r"\${(.*?)}\$",
            custom_fail)  # 查找字符串中所有$key$ 作为关联对象
        for n in relevance_list:  # 遍历关联key
            pattern = re.compile(r'\${' + n + r'}\$')  # 初始化正则匹配
            custom_fail = re.sub(
                pattern,
                relevance[n],
                custom_fail,
                count=1)  # 关联值1次
    except TypeError:
        pass
    return custom_fail


if __name__ == "__main__":
    _custom_fail = "这是一段${test}$用的数据"
    _relevance = {"test": "测试"}
    print(custom_manage(_custom_fail, _relevance))
